package com.itheima.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.mapper.SmsCouponHistoryMapper;
import com.itheima.pojo.SmsCoupon;
import com.itheima.pojo.SmsCouponHistory;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * @author lenovo
 * @description 针对表【sms_coupon_history(优惠券使用、领取历史表)】的数据库操作Service实现
 * @createDate 2022-10-11 14:26:17
 */
@DubboService
public class SmsCouponHistoryApiImpl implements SmsCouponHistoryApi {

    @Autowired
    private SmsCouponHistoryMapper smsCouponHistoryMapper;

    //根据优惠券id，使用状态，订单编号分页获取领取记录
    @Override
    public PageResult couponList(Integer couponId, String orderSn, Integer pageNum, Integer pageSize, Integer useStatus) {
        //有些可能为空要判断下
        //分页
        //分页构造函数
        //Params:
        //pageNum – 当前页
        //pageSize – 每页显示条数
        IPage<SmsCouponHistory> iPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsCouponHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(couponId), SmsCouponHistory::getCouponId, couponId)
                .eq(StrUtil.isNotBlank(orderSn), SmsCouponHistory::getOrderSn, orderSn)
                .eq(ObjectUtil.isNotNull(useStatus), SmsCouponHistory::getUseStatus, useStatus)
                .orderByDesc(SmsCouponHistory::getUseTime);
        smsCouponHistoryMapper.selectPage(iPage, lqw);
        List<SmsCouponHistory> list = iPage.getRecords();
        Integer total = Convert.toInt(iPage.getTotal());//总条数
        Integer totalPage = Convert.toInt(iPage.getPages());//当前分页总页数
        return new PageResult(pageNum, pageSize, total, totalPage, list);
        //    public PageResult(Integer pageNum, Integer pageSize, Integer total, Integer totalPage, List<T> list) {
    }
}




